SqrtGrad
计算 Sqrt(平方根)操作的梯度。该算子是 Sqrt 算子的反向传播(backward pass)部分。
\[\text{output}_i = \frac{1}{2} \times \frac{\text{input1}_i}{\text{input0}_i}\]
其中 input0 是前向传播时 Sqrt 的输出(即 \(y = \sqrt{x}\)),input1 是来自后一层的上游梯度 \(dy\),output 是对原始输入 \(x\) 的梯度 \(dx\)。
- 输入:
input0 - 前向传播时 Sqrt 的输出数据地址(即 \(y = \sqrt{x}\))。
input1 - 来自后一层的上游梯度数据地址(即 \(dy\))。
size - 计算长度。
core_mask - 核掩码(仅共享存储版本需要)。
- 输出:
output - 计算出的对原始输入的梯度数据地址(即 \(dx\))。
- 支持平台:
FT78NEMT7004
备注
FT78NE 支持fp32
MT7004 支持fp16, fp32
共享存储版本:
-
void hp_sqrt_grad_s(half *input0, half *input1, half *output, int size, int core_mask)
-
void fp_sqrt_grad_s(float *input0, float *input1, float *output, int size, int core_mask)
C调用示例:
1//FT78NE示例
2#include <stdio.h>
3#include <sqrt_grad.h>
4
5int main(int argc, char* argv[]) {
6 // 假设在DDR空间
7 float *input0 = (float *)0xA0000000; // Sqrt的输出 y = sqrt(x)
8 float *input1 = (float *)0xA1000000; // 上游梯度 dy
9 float *output = (float *)0xB0000000; // 输出梯度 dx
10
11 int size = 1000;
12 int core_mask = 0xff;
13
14 fp_sqrt_grad_s(input0, input1, output, size, core_mask);
15 return 0;
16}
私有存储版本:
-
void hp_sqrt_grad_p(half *input0, half *input1, half *output, int size)
-
void fp_sqrt_grad_p(float *input0, float *input1, float *output, int size)
C调用示例:
1//FT04示例
2#include <stdio.h>
3#include <sqrt_grad.h>
4
5int main(int argc, char* argv[]) {
6 // 假设在AM空间
7 float *input0 = (float *)0x10000000; // Sqrt的输出 y = sqrt(x)
8 float *input1 = (float *)0x10001000; // 上游梯度 dy
9 float *output = (float *)0x10002000; // 输出梯度 dx
10
11 int size = 1000;
12
13 fp_sqrt_grad_p(input0, input1, output, size);
14 return 0;
15}